/*
 * TOP SECRET Copyright 2006-2015 Transsion.com All right reserved. This software is the confidential and proprietary
 * information of Transsion.com ("Confidential Information"). You shall not disclose such Confidential Information and
 * shall use it only in accordance with the terms of the license agreement you entered into with Transsion.com.
 */
package com.yunji.framework_template.web.service.crawler;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.yunji.framework_template.BaseTestCase;
import com.yunji.framework_template.common.http.WebIconFinder;
import com.yunji.framework_template.orm.persistence.mapper.SitesInfoMapper;
import com.yunji.framework_template.orm.persistence.model.SitesInfo;

/**
 * ClassName:CrawleSietIcon <br/>
 * Date: 2018年11月17日 下午8:39:17 <br/>
 * 
 * @author fenglibin1982@163.com
 * @Blog http://blog.csdn.net/fenglibing
 * @version
 * @see
 */
public class CrawleSietIcon extends BaseTestCase {

    @Autowired
    private SitesInfoMapper sitesInfoMapper;

    @Test
    public void testGetIcon() throws IOException {
        SitesInfo record = new SitesInfo();
        record.setTypeId(76);
        List<SitesInfo> sitesInfoList = sitesInfoMapper.selectByCondition(record);
        for (SitesInfo sitesInfo : sitesInfoList) {
            String url = sitesInfo.getUrl();
            String icon = sitesInfo.getIco();
            if (icon != null && icon.length() > 0) {
                continue;
            }
            icon = WebIconFinder.getIconUrlString(url);
            System.out.println(url + ":" + icon);
            if (icon != null && icon.length() > 0) {
                sitesInfo.setIco(icon);
                sitesInfoMapper.updateByPrimaryKey(sitesInfo);
            }
        }
    }

    @Test
    public void testUpdateIcon() throws IOException {
        List<SitesInfo> sitesInfoList = sitesInfoMapper.selectAll();
        for (SitesInfo sitesInfo : sitesInfoList) {
            String url = sitesInfo.getUrl();
            
            String icon = sitesInfo.getIco();
            if (!(icon == null || icon.length() == 0)) {
                continue;
            }
            System.out.println("Begin to get inco of site:"+url);
            Date now = new Date();
            // 判断是否60分钟之内做过更新
            if ((now.getTime() - sitesInfo.getAdded().getTime()) / (60 * 1000) < 60) {
                continue;
            }
            String newIcon = WebIconFinder.getIconUrlString(url);
            // System.out.println(url + ":old ico:" + sitesInfo.getIco()+":new ico:"+newIcon);
            if (newIcon != null && newIcon.length() > 0 && !icon.equalsIgnoreCase(newIcon)) {
                System.out.println("update url:" + url + ":old ico:" + sitesInfo.getIco() + ": to new ico:" + newIcon);
                sitesInfo.setIco(newIcon);
                sitesInfoMapper.updateByPrimaryKey(sitesInfo);
            }
        }
    }
}
